﻿using System;
using System.ServiceModel;
using AccessControl.FingerPrintLibrary;
using AccessControl.Utility.Tools;


namespace AccessControl.FingerPrintEngine
{
    [ServiceBehavior(
ConcurrencyMode = ConcurrencyMode.Single,
InstanceContextMode = InstanceContextMode.Single)]
    public class FingerPrintEngineService : IFingerPrintEngine, IDisposable
    {
        #region Constructor

        public FingerPrintEngineService()
        {
            try
            {
                Licencing.checkLicence();
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message, "FingerPrintEngineService Constructor", eventLevelConstants.Error);
                
            }
        }

        #endregion

        #region Implementation of IFingerPrintEngine

        public bool EnrollFingerPrint(string pkPerson, byte fingerIndex, byte[][] rawImage, out string errorMessage)
        {
            var error = String.Empty;
            try
            {
                var result = FingerPrintEngine.GetInstance().EnrollPersonFinger(pkPerson, fingerIndex, rawImage, out error);
                errorMessage = error;
                return result;
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message, "EnrollFingerPrint", eventLevelConstants.Error);
                errorMessage = error;
                return false;
            }

        }

        public string IdentifyPerson(byte[] rawImage, Int32 pkEvent, out string errorMessage)
        {
            var error = String.Empty;
            try
            {
                var result = FingerPrintEngine.GetInstance().IdentifyPerson(rawImage, pkEvent, out error);
                errorMessage = error;
                return result;
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message, "IdentifyPersonByEvent", eventLevelConstants.Error);
                errorMessage = error;
                return String.Empty;
            }
        }

        public string IdentifyPerson(Byte[] rawImage, out string errorMessage)
        {
            var error = String.Empty;
            try
            {
                var result = FingerPrintEngine.GetInstance().IdentifyPersonByG(rawImage, out error);
                errorMessage = error;
                return result;
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message, "IdentifyPerson", eventLevelConstants.Error);
                errorMessage = error;
                return String.Empty;
            }
        }

        public bool Verify(string pkPerson, byte[] rawImage, out string errorMessage)
        {
            var error = String.Empty;
            try
            {
                var result = FingerPrintEngine.GetInstance().Verify(pkPerson, rawImage, out error);
                errorMessage = error;
                return result;
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message, "Verify", eventLevelConstants.Error);
                errorMessage = error;
                return false;
            }
        }

        public bool Match(byte[] rawImage1, byte[] rawImage2, out string errorMessage)
        {
            var error = String.Empty;
            try
            {
                var result = FingerPrintEngine.GetInstance().Match(rawImage1, rawImage2, out error);
                errorMessage = error;
                return result;
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message, "Match", eventLevelConstants.Error);
                errorMessage = error;
                return false;
            }
        }

        public void UpdatePesonEvent()
        {
            try
            {
                FingerPrintEngine.GetInstance().ReloadPesonEventDic();
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message, "UpdatePesonEvent", eventLevelConstants.Error);
            }

        }

        #endregion

        #region Implementation of IDisposable

        public void Dispose()
        {
            try
            {
                Licencing.ReleaseLicence();

            }
            catch (Exception ex)
            {

                WriteLog(ex.Message, "Dispose", eventLevelConstants.Error);
            }
        }

        #endregion

        #region Private Method

        private static void WriteLog(String message, String callerMethod, eventLevelConstants level)
        {
            LogFile.WriteLog(LogFile.DefaultLogfileLocation + "FingerPrintService\\" + DateTime.Now.Date.ToString("yyyyMMdd") + ' ' + callerMethod + ".txt", level, message);
        }

        #endregion
    }
}
